package cn.edu.nuc.ssm.controller;

import cn.edu.nuc.ssm.entity.User;
import cn.edu.nuc.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2017/4/6.
 */
@Controller
@RequestMapping("/front")
public class FrontLoginController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/tologin")
    public String toLogin(){

        return "front/login";
    }
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(String username, String password, HttpSession session, Model model,RedirectAttributes attr){


        User user = userService.login(username, password);

        if (user==null){
          //  model.addAttribute("msg","<font color='red'>用户名、密码错误</font>");
            attr.addAttribute("msg","error");
            return "redirect:/front/tologin";
        }else if(user.getStatus()==0){
            attr.addAttribute("msg","freeze");
            return "redirect:/front/tologin";
        }
        model.addAttribute("flag",true);
        session.setAttribute("user",user);
        return "redirect:/jizhang/index";
    }

    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public String logout( HttpSession session){

        session.removeAttribute("user");
        return "redirect:/front/tologin";
    }

}
